package com.gzyj.jtt808.v2013.protocol;

import com.alibaba.fastjson.annotation.JSONField;
import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.protocol.terminalparam.*;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.BusinessDataType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class JTT808_0x8103 extends Package implements Serializable {
    private List<JTT808_0x8103_TerminalParam> terminalParams;

    public JTT808_0x8103() {
        super(BusinessDataType.设置终端参数.getCode());

        params.put(0x0002, JTT808_0x8103_0x0002.class.getName());
        params.put(0x0003, JTT808_0x8103_0x0003.class.getName());
        params.put(0x0004, JTT808_0x8103_0x0004.class.getName());
        params.put(0x0005, JTT808_0x8103_0x0005.class.getName());
        params.put(0x0006, JTT808_0x8103_0x0006.class.getName());
        params.put(0x0007, JTT808_0x8103_0x0007.class.getName());
        params.put(0x0010, JTT808_0x8103_0x0010.class.getName());
        params.put(0x0011, JTT808_0x8103_0x0011.class.getName());
        params.put(0x0012, JTT808_0x8103_0x0012.class.getName());
        params.put(0x0013, JTT808_0x8103_0x0013.class.getName());
        params.put(0x0014, JTT808_0x8103_0x0014.class.getName());
        params.put(0x0015, JTT808_0x8103_0x0015.class.getName());
        params.put(0x0016, JTT808_0x8103_0x0016.class.getName());
        params.put(0x0017, JTT808_0x8103_0x0017.class.getName());



    }

    @JSONField(serialize = false)
    private Map<Integer, String> params = new HashMap<>();


    @Override
    public void encodeImpl(ByteBuf buf) {
        buf.writeByte(this.terminalParams.size());
        for (JTT808_0x8103_TerminalParam param : this.terminalParams) {
            param.encode(buf);
        }
    }

    @Override
    public JTT808_0x8103 decodeImpl(ByteBuf buf) {
        byte[] temp = HexBytesUtil.readByOffset(buf, 1);
        int count = Integer.parseInt(HexBytesUtil.bytesToHex(temp), 16);
        this.terminalParams = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            ByteBuf byteBuf = Unpooled.copiedBuffer(buf);
            temp = HexBytesUtil.readByOffset(byteBuf, 4);
            int id = HexBytesUtil.byteArrayToInt(temp);


            try {
                JTT808_0x8103_TerminalParam param = (JTT808_0x8103_TerminalParam) Class.forName(this.params.get(id)).newInstance();
                param.decode(buf);

                this.terminalParams.add(param);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }


        }

        return this;
    }

}
